package page.sample;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import page.MyFirstWebPage;
import dao.impl.FlightDao;
import dao.impl.TicketDao;
import dao.impl.UserDao;
import entity.Flight;
import entity.Ticket;
import entity.User;

public class TicketPage extends WebPage {

	public TicketPage(final User user, final Flight flight) {
		final TicketDao ticketDao = new TicketDao(
				app.MyFirstWebApplication.DATA_XML_FOLDER);
		final UserDao userDao = new UserDao(
				app.MyFirstWebApplication.DATA_XML_FOLDER);
		final Ticket newTicket = new Ticket();

		add(new Link("ShowFlight") {

			@Override
			public void onClick() {
				setResponsePage(new FlightPage(user));
			}

		});

		add(new Link("ShowTicket") {

			@Override
			public void onClick() {
				setResponsePage(new ShowTicketPage(user));

			}

		});

		add(new Link("exit") {

			@Override
			public void onClick() {
				setResponsePage(new MyFirstWebPage());

			}

		});

		add(new Label("name", flight.getName()));
		add(new Label("date", flight.getDate()));
		add(new Label("time", flight.getTime()));
		add(new Label("price", flight.getPrice()));
		add(new Link("baggageYes") {
			@Override
			public void onClick() {
				newTicket.setBaggage(true);
			}
		});

		add(new Link("baggageNo") {
			@Override
			public void onClick() {
				newTicket.setBaggage(false);
			}
		});

		add(new Link("firstRegistrationYes") {
			@Override
			public void onClick() {
				newTicket.setFirstRegistration(true);
				newTicket.setPrice(flight.getPrice() + (flight.getPrice() / 4));
			}
		});

		add(new Link("firstRegistrationNo") {
			@Override
			public void onClick() {
				newTicket.setFirstRegistration(false);
				newTicket.setPrice(flight.getPrice());
			}
		});

		// add button component to open "edit" page
		add(new Link("reservation") {
			@Override
			public void onClick() {
				newTicket.setNameFlight(flight.getName());
				newTicket.setDate(flight.getDate());
				newTicket.setTime(flight.getTime());
				
				user.getTickets().add(newTicket);
				
				userDao.update(user);
				
				setResponsePage(new FlightPage(user));
			}
		});
	}
}
